package com.xjj.controller;

import cn.hutool.core.codec.Base64;
import com.xjj.QRCode.QRCodeUtils;
import com.xjj.entity.Klass;
import com.xjj.entity.ResponseEntity;
import com.xjj.service.KlassService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.UUID;

/**
 * @author sxy
 * @version 1.0
 * @date 2022/10/20 15:11
 */
@RestController
@RequestMapping("/manage/admin/klass")
public class KlassContorller {
    @Resource
    private KlassService klassService;

    @PostMapping("/addKlass")
    @ApiOperation(value = "添加班级")
    public ResponseEntity addKlass(@RequestBody Klass klass) throws Exception {

        String uuid = UUID.randomUUID().toString().replace("-", "a");
        BufferedImage image = QRCodeUtils.createImage(uuid);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", stream);
        String base64 = Base64.encode(stream.toByteArray());
        stream.flush();
        stream.close();
        klass.setKlassVerify(uuid);
        boolean save = klassService.save(klass);
        return new ResponseEntity<>(200,"ok",save);
    }

    @DeleteMapping("/deleteKlassById/{id}")
    @ApiOperation(value = "通过id删除班级")
    public ResponseEntity deleteKlassById(@PathVariable int id){
        boolean flag = klassService.removeById(id);
        return new ResponseEntity<>(200,"ok",flag);
    }

    @ApiOperation(value = "查找所有班级所有信息")
    @GetMapping("/findAllKlass")
    public ResponseEntity findAllKlass(){
        List<Klass> klasses = klassService.list();
        return new ResponseEntity<>(200,"ok",klasses);
    }

    @PutMapping("/addJobNumById")
    @ApiOperation(value = "根据班级id增加1当前班级的作业数量")
    public ResponseEntity addJobNumById(@RequestBody Klass klass){
        klassService.addJobNumById(klass);
        return ResponseEntity.SUCCESS("ok",200);
    }

    @PutMapping("subJobNumById")
    @ApiOperation(value = "根据班级id减少jobNum当前班级的作业数量")
    public ResponseEntity subJobNumById( int jobNum,int id){
        klassService.subJobNumBuId(jobNum,id);
        return ResponseEntity.SUCCESS("ok",200);
    }

    @GetMapping("/getKlassById/{id}")
    @ApiOperation(value = "根据班级id查询班级信息")
    public ResponseEntity getKlassById(@PathVariable int id){
        Klass klassById = klassService.getKlassById(id);
        return new ResponseEntity(200,"ok",klassById);
    }

    @GetMapping("getKlassByManagerId/{id}")
    @ApiOperation(value = "根据老师id查询相关班级")
    public ResponseEntity getKlassByManagerId(@PathVariable int id){
        List<Klass> klassByManagerId = klassService.getKlassByManagerId(id);
        return new ResponseEntity(200,"ok",klassByManagerId);
    }

    @GetMapping("/getKlassByName/{classname}")
    @ApiOperation("根据班级名称查询班级是否存在")
    public ResponseEntity isExist(@PathVariable("classname") String classname){
        return new ResponseEntity(200,"ok",klassService.isExist(classname));
    }

    @GetMapping("/getKlassByMasterId/{id}")
    @ApiOperation(value = "根据班主任id查询所管理班级")
    public ResponseEntity getKlassByMasterId(@PathVariable int id){
        Klass klass = klassService.getKlassByMasterId(id);
        return new ResponseEntity(200,"ok",klass);
    }

    @PutMapping("/updateVerifyById")
    public ResponseEntity updateVerifyById(@RequestBody Klass klass){
        klassService.updateVerifyById(klass.getKlassVerify(),klass.getId());
        return ResponseEntity.SUCCESS("ok",200);
    }
}
